Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

XFuConfiguration.cpp

Go to the documentation of this file.
00001 /*! \file
00002  * X-Forge Util <br>
00003  * Copyright 2000-2003 Fathammer Ltd
00004  *
00005  * \brief XFuConfiguration.cpp is the implementation file for the XFuConfiguration
00006  * class. For more information, see XFuConfiguration.h
00007  *
00008  * $Id: XFuConfiguration.cpp,v 1.17 2003/05/02 10:24:19 kkallio Exp $
00009  * $Date: 2003/05/02 10:24:19 $
00010  * $Revision: 1.17 $
00011  */
00012 
00013 #include <xfcore/XFcCore.h>
00014 #include <xfutil/XFuConfiguration.h>
00015 
00016 
00017 XFuConfiguration * XFuConfiguration::create()
00018 {
00019     return new XFuConfiguration;
00020 }
00021 
00022 
00023 XFuConfiguration * XFuConfiguration::create(const CHAR *aFilename)
00024 {
00025     XFuConfiguration *config = new XFuConfiguration;
00026     if (config != NULL)
00027     {
00028         if (!config->load(aFilename))
00029         {
00030             delete config;
00031             return NULL;
00032         }
00033     }
00034     return config;
00035 }
00036 
00037 
00038 XFuConfiguration * XFuConfiguration::create(XFcFile *aFile)
00039 {
00040     XFuConfiguration *config = new XFuConfiguration;
00041     if (config != NULL)
00042     {
00043         if (!config->load(aFile))
00044         {
00045             delete config;
00046             return NULL;
00047         }
00048     }
00049     return config;
00050 }
00051 
00052 
00053 XFuConfiguration::XFuConfiguration() : mProperties(NULL)
00054 {
00055 }
00056 
00057 
00058 XFuConfiguration::~XFuConfiguration()
00059 {
00060     clear();
00061 }
00062 
00063 
00064 CHAR * XFuConfiguration::get(const CHAR *aKey)
00065 {
00066     if (mProperties == NULL) return NULL;
00067     XFcHashtableIterator<XFuStringKey, void *> it = mProperties->find(XFuStringKey(aKey));
00068     return (it.isValid() ? (CHAR*)it.getValue() : NULL);
00069 }
00070 
00071 
00072 INT32 XFuConfiguration::getINT32(const CHAR *aKey)
00073 {
00074     CHAR *value = get(aKey);
00075     if (value == NULL)
00076         return 0;
00077     else
00078         return XFcStringToolkit::toINT32(value);
00079 }
00080 
00081 
00082 FLOAT32 XFuConfiguration::getFLOAT32(const CHAR *aKey)
00083 {
00084     CHAR *value = get(aKey);
00085     if (value == NULL)
00086         return 0;
00087     else
00088         return XFcStringToolkit::toFLOAT32(value);
00089 }
00090 
00091 
00092 void XFuConfiguration::put(const CHAR *aKey, const CHAR *aValue)
00093 {
00094     if (mProperties == NULL || aKey == NULL || aValue == NULL) return;
00095     CHAR *oldVal = get(aKey);
00096     if (oldVal != NULL)
00097     {
00098         delete[] get(aKey);
00099         mProperties->remove(XFuStringKey(aKey));
00100     }
00101     CHAR * val = XFcStringToolkit::copy(aValue);
00102     mProperties->put(XFuStringKey(aKey), (void *)val);
00103 }
00104 
00105 
00106 void XFuConfiguration::clear()
00107 {
00108     if (mProperties != NULL)
00109     {
00110         XFcHashtableIterator<XFuStringKey, void *> it;
00111         for (it = mProperties->begin(); it != mProperties->end(); ++it)
00112             delete[] (CHAR*)it.getValue();
00113         delete mProperties;
00114         mProperties = NULL;
00115     }
00116 }
00117 
00118 
00119 INT XFuConfiguration::load(XFcFile *aFile)
00120 {
00121     if (aFile == NULL)
00122         return 0;
00123 
00124     if (mProperties == NULL)
00125         mProperties = new XFcHashtable<XFuStringKey, void *>;
00126     if (mProperties == NULL)
00127         return 0;
00128 
00129     // check length of rest of the file
00130     INT32 orgFilePos = aFile->tell();
00131     aFile->seek(0, SEEK_END);
00132     INT32 fileLen = aFile->tell();
00133     aFile->seek(orgFilePos, SEEK_SET);
00134 
00135     // read until end of file
00136     while (aFile->tell() < fileLen)
00137     {
00138         // read a line, covert it to 16 bit and remove leading and trailing white spaces
00139         CHAR8 *buffer8 = buffer8 = readLine(aFile);
00140         CHAR *buffer = XFcStringToolkit::copy(buffer8);
00141         CHAR *trimmedBuffer = strDupTrim(buffer);
00142 
00143         // parse the line and add the property key and value to the property hashtable
00144         processLine(trimmedBuffer);
00145 
00146         delete[] trimmedBuffer;
00147         delete[] buffer;
00148         delete[] buffer8;
00149     }
00150 
00151     return 1;
00152 }
00153 
00154 
00155 INT XFuConfiguration::load(const CHAR *aFilename)
00156 {
00157     XFcFile *file = XFcFile::open(aFilename, XFCSTR("rb"));
00158     if (file == NULL)
00159         return 0;
00160 
00161     INT result = load(file);
00162    
00163     file->close();
00164 
00165     return result;
00166 }
00167 
00168 
00169 void XFuConfiguration::processLine(const CHAR *aStr)
00170 {
00171 
00172     // valid property definition?
00173     INT32 len = XFcStringToolkit::getLength(aStr);
00174     if (len > 0 && aStr[0] != '#')
00175     {
00176 
00177         // find the first = char
00178         INT32 valStart = 0;
00179         while (valStart < len && aStr[valStart] != '=') valStart++;
00180 
00181         if (valStart != len && valStart != 0)
00182         {
00183 
00184             INT32 i, p;
00185 
00186             CHAR * key = new CHAR[valStart + 1];
00187 
00188             p = 0;
00189             for (i = 0; i < valStart; i++) key[p++] = aStr[i];
00190             // find last non white space char of the key
00191             do --p; while (key[p] == ' ' || key[p] == '\t');
00192             key[p + 1] = '\0';
00193             
00194             // find first non white space char of the value
00195             do valStart++; while (aStr[valStart] == ' ' || aStr[valStart] == '\t');
00196 
00197             CHAR * val = new CHAR[len - valStart + 1];
00198 
00199             p = 0;
00200             for (i = valStart; i < len; i++) val[p++] = aStr[i];
00201             val[p] = '\0';
00202 
00203             put(key, val);
00204 
00205             delete[] key;
00206             delete[] val;
00207 
00208         }
00209 
00210     }
00211 
00212 }
00213 
00214 
00215 CHAR * XFuConfiguration::strDupTrim(const CHAR *aStr)
00216 {
00217 
00218     INT32 len = XFcStringToolkit::getLength(aStr);
00219 
00220     // find first non white space char
00221     INT32 start = 0;
00222     while (start < len && (aStr[start] == ' ' || aStr[start] == '\t'))
00223         start++;
00224 
00225     // find last non white space char
00226     INT32 end = len - 1;
00227     while (end >= 0 && (aStr[end] == ' ' || aStr[end] == '\t'))
00228         end--;
00229 
00230     CHAR *buf;
00231     INT32 p = 0;
00232     if (end == -1)
00233     {
00234         // only spaces so the result is an empty string
00235         buf = new CHAR[1];
00236     }
00237     else
00238     {
00239         // copy the chars between the first and the last white space
00240         buf = new CHAR[end - start + 2];
00241         INT32 i;
00242         for (i = start; i <= end; i++) buf[p++] = aStr[i];
00243     }
00244 
00245     buf[p] = '\0';
00246     return buf;
00247 
00248 }
00249 
00250 
00251 CHAR8 * XFuConfiguration::readLine(XFcFile *aFile)
00252 {
00253 
00254     INT32 currentPos = aFile->tell();
00255 
00256     INT32 count = 0;
00257     CHAR8 c = 0;
00258 
00259     // find end of line
00260     while (c != 10)
00261     {
00262         if (aFile->read(&c, sizeof(CHAR8), 1) == 0) break;
00263         count++;
00264     }
00265 
00266     aFile->seek(currentPos, SEEK_SET);
00267 
00268     // read until end of line
00269     CHAR8 *buffer = new CHAR8[count + 1];
00270     if (count > 0) aFile->read(buffer, sizeof(CHAR8), count);
00271 
00272     buffer[count] = '\0';
00273 
00274     // remove trailing CR and LF
00275     if (count >= 1 && buffer[count - 1] == 10) buffer[count - 1] = '\0';
00276     if (count >= 2 && buffer[count - 2] == 13) buffer[count - 2] = '\0';
00277 
00278     return buffer;
00279 
00280 }

   
X-Forge Documentation
Confidential
Copyright © 2002-2003 Fathammer
   
Documentation generated
with doxygen
by Dimitri van Heesch